package client.gui;

import shared.Consts;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import client.ClientController;
import packet.StartGameReq;

/**
 * Gui for InEquity lobby
 * @author Joel Garboden
 */
public class ClientGUI_Lobby extends JFrame
{
  private static final String DEFAULT_NAME = "Name";

  private ClientController controller;

  private JTextField nameBox;
  private JTextField connInfo;
  private JButton connect;
  private JButton startGame;
  private JButton quit;

  private ChatComponent chatComponent;

  /**
   * Constructor
   * @param controller reference to gui owner
   */
  public ClientGUI_Lobby(ClientController controller)
  {

    setTitle(Consts.LOBBY_TITLE);
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    setMinimumSize(new Dimension(600, 310));
    setPreferredSize(new Dimension(600, 310));
    setLayout(new BorderLayout());

    nameBox = new JTextField(DEFAULT_NAME);
    nameBox.setColumns(12);

    connInfo = new JTextField(Consts.DEFAULT_IP + ":" + Consts.DEFAULT_PORT);
    connect = new JButton("Connect");

    quit = new JButton("Quit");

    quit.addActionListener(new java.awt.event.ActionListener()
    {
      @Override
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        quit();
      }
    });

    startGame = new JButton("Start Game");

    startGame.addActionListener(new java.awt.event.ActionListener()
    {
      @Override
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        startGame();
      }
    });

    Box optionsBar = Box.createHorizontalBox();
    optionsBar.add(nameBox);
    optionsBar.add(connInfo);

    connInfo.setEditable(true);
    optionsBar.add(connect);

    connect.addActionListener(new java.awt.event.ActionListener()
    {
      @Override
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        connectActionPerformed(evt);
      }
    });

    optionsBar.add(startGame);
    optionsBar.add(quit);

    add(optionsBar, BorderLayout.NORTH);

    this.controller = controller;

    chatComponent = new ChatComponent(controller, new Dimension(100, 200));
    chatComponent.setLayout(new GridLayout(2, 1));

    add(chatComponent, BorderLayout.SOUTH);

    startGame.setEnabled(false);

    setLocationRelativeTo(null);
  }

  /**
   * Posts chat to gui
   * @param name senders name
   * @param chatMsg incoming chat message
   */
  public void postChat(String name, String chatMsg)
  {
    chatComponent.postChat(name, chatMsg);
  }

  /**
   * Mutator
   * @param toggle chat active state
   */
  public void toggleChat(boolean toggle)
  {
    chatComponent.enableChat(toggle);
  }

  /**
   * ActionPerformed event function for connect button
   * @param evt action event performed
   */
  private void connectActionPerformed(java.awt.event.ActionEvent evt)
  {
    String connectionInfo[];
    String delims = ":";
    String host;
    int port;
    String name;
    try
    {
      if(!nameBox.getText().equals(""))
      {
        name = nameBox.getText();
      }
      else
      {
        return;
      }

      if(nameBox.getText().equals(DEFAULT_NAME))
      {
        return;
      }

      controller.setPlayerName(name);

      connectionInfo = connInfo.getText().split(delims);

      host = connectionInfo[0];
      port = Integer.parseInt(connectionInfo[1]);

      if(!controller.connectToServer(name, host, port))
      {
        connect.setEnabled(true);
        nameBox.setEnabled(true);
        connInfo.setEnabled(true);
      }
      else
      {
        nameBox.setEnabled(false);
        connect.setEnabled(false);
        chatComponent.enableChat(true);
        startGame.setEnabled(true);
        connInfo.setEnabled(false);
      }
    }
    catch(Exception e)
    {
      connect.setEnabled(true);
      Logger.getLogger(ClientGUI_Lobby.class.getName()).log(Level.SEVERE, "LobbyGUI.connect()", e);
    }
  }

  /**
   * If connected: queries the server for quit authorization<br/>
   * otherwise, disposes gui
   */
  public void quit()
  {
    if(connect.isEnabled())
    {
      dispose();
    }
    else
    {
      controller.quit();
    }
  }

  /**
   * Requests server for a game start
   */
  public void startGame()
  {
    controller.sendRequest(new StartGameReq());
  }
}
